$(document).ready(function () {
  mini.parse(); // 初始化

  var $searchForm = $('#searchForm'),
      $create = $('#create'),
      $edit = $('#edit'),
      $role = $('#role'),
      $remove = $('#remove'),
      $search = $('#search'),
      table = mini.get("table");

  // 按钮状态
  table.on('cellclick', function (grid) {
    if (grid) {
      $edit.removeAttr('disabled');
      $role.removeAttr('disabled');
      $remove.removeAttr('disabled');
    }
  });

  // 搜索
  $search.click(function () {
    search(table);
  });

  // 新增
  $create.click(function () {
    form({
      title: '新增用户',
      url: '/role/form.html',
      h: '360',
      table: table
    });
  });

  // 编辑
  $edit.click(function () {
    var row = table.getSelected();
    if (row) {
      form({
        title: '编辑用户',
        url: '/role/form.html',
        h: '360',
        row: row,
        table: table
      });
    }
  });

  // 删除
  $remove.click(function () {
    var row = table.getSelected();
    if (row) {
      mini.alert('确认删除？', '删除用户', function (action) {
        if (action === 'ok') {
          Ajax.Delete('delete', JSON.stringify([row.id]), function (result) {
            if (result.code === 200) {
              table.removeRow(row, true);
            } else {
              mini.alert(result.msg);
            }
          });
        }
      });
    }
  });

  // 权限
  $role.click(function () {
    var row = table.getSelected();
    if (row) {
      form({
        title: '角色分配',
        url: '/role/role-form.html?row_id=' + row.id,
        row: row,
        table: table
      });
    }
  });
});